package com.bingxue.edu.parent.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.management.shop.model.query.ParentCouponQuery;
import com.bingxue.edu.management.shop.service.CartService;
import com.bingxue.edu.management.shop.service.CouponService;
import com.bingxue.edu.parent.model.query.ParentShopClassQuery;
import com.bingxue.edu.parent.model.req.ParentCartItemAddReq;
import com.bingxue.edu.parent.model.resp.ParentCartItem;
import com.bingxue.edu.parent.model.resp.ParentCouponDetailResp;
import com.bingxue.edu.parent.model.resp.StudentShopClassResp;
import com.bingxue.edu.parent.service.ParentShopService;
import com.bingxue.edu.parent.util.ParentAuthUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author panbo
 * @since 2024/10/28
 */
@Tag(name = "家长端小程序-购课接口")
@RestController
@RequestMapping("/p/shop")
@RequiredArgsConstructor
public class ParentShopController extends ParentBaseController {

    private final ParentShopService parentShopService;
    private final CartService cartService;
    private final CouponService couponService;

    @Operation(summary = "查询购课班级")
    @GetMapping("/class/search")
    public Response<Pagination<StudentShopClassResp>> searchClass(@Validated ParentShopClassQuery query, @Validated PageQuery pageQuery) {
        return Response.success(parentShopService.getShopClassPage(query, pageQuery));
    }

    @Operation(summary = "查询购课班级")
    @GetMapping("/class/list")
    public Response<List<StudentShopClassResp>> getShopClassList(@Validated ParentShopClassQuery query) {
        return Response.success(parentShopService.getShopClassList(query));
    }

    @Operation(summary = "查询购课班级详情")
    @GetMapping("/class/detail/{classId}")
    public Response<StudentShopClassResp> getShopClassById(@PathVariable String classId) {
        return Response.success(parentShopService.getShopClassById(classId));
    }

    @Operation(summary = "加入购物车")
    @PostMapping("/cart/add")
    public Response<Void> addCartItem(@RequestBody ParentCartItemAddReq req) {
        this.checkStudentRelation(req.getStudentId());
        cartService.addCartItem(req,true);
        return Response.success();
    }

    @Operation(summary = "移除购物车")
    @PostMapping("/cart/remove/{id}")
    public Response<Void> removeCardItem(@PathVariable String id) {
        cartService.removeCardItem(id);
        return Response.success();
    }

    @Operation(summary = "查询购物车")
    @GetMapping("/cart/get/{studentId}")
    public Response<List<ParentCartItem>> getCardItemList(@PathVariable String studentId) {
        this.checkStudentRelation(studentId);
        return Response.success(cartService.getCardItemList(studentId));
    }

    @Operation(summary = "查询购物车数量")
    @GetMapping("/cart/count/{studentId}")
    public Response<Long> getCardItemCount(@PathVariable String studentId) {
        this.checkStudentRelation(studentId);
        return Response.success(cartService.getCardItemCount(studentId));
    }

    @Operation(summary = "查询优惠券")
    @GetMapping("/coupon/list")
    public Response<List<ParentCouponDetailResp>> getCouponList(@Validated ParentCouponQuery query) {
        query.setParentId(ParentAuthUtil.getCurrentParentId());
        List<ParentCouponDetailResp> couponList = couponService.getParentCouponList(query);
        return Response.success(couponList);
    }
}
